package com.aids_malaria.app;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;

import com.aids_malaria.R;
import com.aids_malaria.util.LogUtil;
import com.aids_malaria.util.ResourceUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

public class App extends Application {

    private static App mApplication;
    protected static Context mContext;
    protected static Handler sHandler;
    protected static int sMainThreadId;

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        mContext = getApplicationContext();
        sHandler = new Handler();
        sMainThreadId = android.os.Process.myTid();
        initRefreshLayout();
        LogUtil.init(true);
    }

    private void initRefreshLayout() {
        //初始化SmartRefreshLayout样式
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                layout.setPrimaryColors(ResourceUtil.getColor(R.color.c_white), ResourceUtil.getColor(R.color.text_gray));
                return new ClassicsHeader(context);
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context);
            }
        });
    }

    public static App getInstance(){
        return mApplication;
    }

    /**
     * 获取全局context
     * @return
     */
    public static Context getAppContext(){
        return mContext;
    }

    /**
     * 获取全局handler
     * @return
     */
    public static Handler getHandler(){
        return sHandler;
    }

    /**
     * 获取主线程id
     * @return
     */
    public static int getMainThreadId(){
        return sMainThreadId;
    }

}
